﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace DAL.Workflows
{
    public class ComponentDALC
    {
        public static void InsertComponent(ComponentReflector componentReflector)
        {
            Database db = DatabaseFactory.CreateDatabase("Workflow");
            db.ExecuteNonQuery("ComponentInsert",
                               componentReflector.ComponentID,
                               componentReflector.WorkflowID,
                               componentReflector.AssemblyFileName,
                               componentReflector.ComponentTypeName);
        }

        public static ComponentReflector GetComponent(Guid componentID)
        {
            Database db = DatabaseFactory.CreateDatabase("Workflow");
            ComponentReflector componentReflector=new ComponentReflector();
            IDataReader reader = db.ExecuteReader("ComponentGet", componentID);
            if(reader.Read())
            {
                Guid workflowID = new Guid(reader["WorkflowID"].ToString());
                string assemblyFileName = reader["AssemblyFileName"].ToString();
                string componentTypeName = reader["ComponentTypeName"].ToString();
                componentReflector = new ComponentReflector()
                                         {
                                             ComponentID = componentID,
                                             WorkflowID = workflowID,
                                             AssemblyFileName = assemblyFileName,
                                             ComponentTypeName = componentTypeName
                                         };
            }
            reader.Close();
            return componentReflector;
        }

        public static List<ComponentReflector> GetComponentsByWorkflow(Guid workflowID)
        {
            List<ComponentReflector> components=new List<ComponentReflector>();
            Database db = DatabaseFactory.CreateDatabase("Workflow");
            IDataReader reader = db.ExecuteReader("ComponentGetByWorkflow", workflowID);
            while (reader.Read())
            {
                Guid componentID = new Guid(reader["ComponentID"].ToString());
                string assemblyFileName = reader["AssemblyFileName"].ToString();
                string componentTypeName = reader["ComponentTypeName"].ToString();
                ComponentReflector componentReflector =
                    new ComponentReflector()
                        {
                            ComponentID = componentID,
                            WorkflowID = workflowID,
                            AssemblyFileName = assemblyFileName,
                            ComponentTypeName = componentTypeName
                        };
                components.Add(componentReflector);
            }
            reader.Close();
            return components;
        }
    }
}
